package com.example.lzy.listviewsample.com.example.lzy.listviewsample.zdy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.lzy.listviewsample.R;

import java.util.ArrayList;
import java.util.List;

public class ZdyListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_item_layout);

        ListView listView = (ListView) findViewById(R.id.listview);

        final List<String> components = new ArrayList<>();
        components.add("1");
        components.add("2");
        components.add("3");
        components.add("4");
        components.add("5");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.custom_item,R.id.texts,components);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(ZdyListViewActivity.this, components.get(i), Toast.LENGTH_SHORT).show();
            }
        });

    }
}
